import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login/login'
import layout from '@/views/layout/layout'
import business from '@/views/layout/business/business'
import chart from '@/views/layout/chart/chart'
import question from '@/views/layout/question/question'
import subject from '@/views/layout/subject/subject'
import user from '@/views/layout/user/user'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'
import { Message } from 'element-ui'
import { removeLocal } from '@/utils/local'
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    {
      path: '/login',
      component: login,
      meta: { title: '登录', roles: ['超级管理员', '管理员', '老师', '学生'] }
    },
    {
      path: '/layout',
      component: layout,
      redirect: '/layout/subject',
      children: [
        {
          path: '/layout/business',
          component: business,
          meta: { title: '企业列表', roles: ['超级管理员', '管理员', '老师'] }
        },
        {
          path: '/layout/chart',
          component: chart,
          meta: { title: '数据概览', roles: ['超级管理员', '管理员', '老师'] }
        },
        {
          path: '/layout/question',
          component: question,
          meta: { title: '题库列表', roles: ['超级管理员', '管理员', '老师'] }
        },
        {
          path: '/layout/subject',
          component: subject,
          meta: {
            title: '学科列表',
            roles: ['超级管理员', '管理员', '老师', '学生']
          }
        },
        {
          path: '/layout/user',
          component: user,
          meta: { title: '用户列表', roles: ['超级管理员', '管理员'] }
        }
      ]
    }
  ]
})
// 路由前置守卫
router.beforeEach((to, from, next) => {
  Nprogress.start()
  if (to.meta.roles.includes(store.state.role)) {
    store.state.jurisdiction = 1
    next()
  } else {
    store.state.jurisdiction = 0
    Message.error('您的权限不够,无法访问该网页')
    removeLocal('token')
    next('/login')
  }
})
// 路由后置守卫
router.afterEach(to => {
  Nprogress.done()
  document.title = to.meta.title
})
export default router
